package com.hexiang.service.impl;

import com.hexiang.client.UserHttpApiClient;
import com.hexiang.domain.CommonResponse;
import com.hexiang.domain.User;
import com.hexiang.service.UserHttpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class UserHttpServiceImpl implements UserHttpService {

    @Resource
    private UserHttpApiClient userHttpApiClient;

    @Override
    public CommonResponse getUsers() {
        CommonResponse commonResponse;

        try {
            List<User> users = userHttpApiClient.getUsers();
            commonResponse = CommonResponse.builder().body((ArrayList) users).succ(true).build();
        } catch (Exception e) {
            log.error("getUsersErr", e);
            commonResponse = CommonResponse.builder().code(500).msg("调用服务报错").succ(false).build();
        }

        return commonResponse;
    }

    @Override
    public CommonResponse getUserByName(String name) {
        CommonResponse commonResponse;

        try {
            return CommonResponse.builder().succ(true).body(userHttpApiClient.getUserByName(name)).build();
        } catch (Exception e) {
            log.error("getUserByNameErr,request={}", name, e);
            commonResponse = CommonResponse.builder().code(500).msg("调用服务报错").succ(false).build();
        }
        return commonResponse;
    }

    @Override
    public CommonResponse addUser(String name, int age) {
        CommonResponse commonResponse;

        try {
            Map<String,Object> params=new HashMap<>();
            params.put("name",name);
            params.put("age",age);
            return CommonResponse.builder().succ(true).body(userHttpApiClient.addUser(params)).build();
        } catch (Exception e) {
            log.error("addUserErr,request={}", String.format("name=%s,age=%s", name, age), e);
            commonResponse = CommonResponse.builder().code(500).msg("调用服务报错").succ(false).build();
        }
        return commonResponse;

    }

    @Override
    public CommonResponse deleteUserById(Long id) {
        CommonResponse commonResponse;

        try {
            return CommonResponse.builder().succ(true).body(userHttpApiClient.deleteUserById(id)).build();
        } catch (Exception e) {
            log.error("deleteUserByIdErr,request={}", id, e);
            commonResponse = CommonResponse.builder().code(500).msg("调用服务报错").succ(false).build();
        }
        return commonResponse;
    }

    @Override
    public CommonResponse<Serializable> updateUser(User user) {
        CommonResponse commonResponse;
        try {
            return CommonResponse.builder().succ(true).body(userHttpApiClient.updateUser(user)).build();
        } catch (Exception e) {
            log.error("deleteUserByIdErr,request={}", user, e);
            commonResponse = CommonResponse.builder().code(500).msg("调用服务报错").succ(false).build();
        }
        return commonResponse;
    }

    @Override
    public CommonResponse<Serializable> getUserPage(Integer pageNum, Integer pageSize) {
        CommonResponse commonResponse;
        try {
            return CommonResponse.builder().succ(true).body(userHttpApiClient.getUserPage(pageNum, pageSize)).build();
        } catch (Exception e) {
            log.error("getUserPageErr,request={}", String.format("pageNum=%s,pageSize=%s", pageNum, pageSize), e);
            commonResponse = CommonResponse.builder().code(500).msg("调用服务报错").succ(false).build();
        }
        return commonResponse;
    }
}